package com.example.management.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class WebConfig {

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();

        // 设置允许跨域的域名
        config.addAllowedOrigin("http://localhost:8080");
        // 设置允许的方法类型
        config.addAllowedMethod("*");
        // 设置允许的请求头
        config.addAllowedHeader("*");
        // 设置是否允许携带Cookie
        config.setAllowCredentials(true);

        // 注册配置
        source.registerCorsConfiguration( "/**", config);

        // 创建并返回CorsFilter
        return new CorsFilter(source);
    }
}